REM Multi FRAPS files batch encoding to mkv AVC1 PART10
REM by Jacklemage July 2014
REM v1.0

@echo off
set debug=0
set shutdown=0

REM Needed stuff to be downloaded & installed
REM http://www.x264.nl/x264_main.php/
REM http://ffmpeg.zeranoe.com/builds/win64/static/

REM Remember to put ffmpeg & x264 executables in the Windows $PATH environment

REM USAGE :
REM 1/ Open a command line window (Windows start menu exec: command)
REM 2/ Change directory to the one having FRAPS files
REM 3/ execute multiFrapsTOx264.bat
REM 4/ Let the script do its jobs
REM 5/ That's all folks!

REM ###########################################################
REM #LOOP : FRAPS files in dir to be encoded & encoder options
REM ###########################################################
for %%f in (*.avi) do (
	call :intro %debug%
	call :audio "%%f"	
	
	REM :video "[file]" [CRF:{16-23}] [x264_preset:{ultrafast|superfast|veryfast|faster|medium|slow|slower|veryslow|placebo}] [process_priority:{high|normal|low}]
	call :video "%%f" 16 slower low
	call :muxing "%%f"
	call :next
)
if %shutdown% == 1 shutdown -s
goto :eof


REM #################################
REM #STEPS : 1/AUDIO 2/VIDEO 3/MUX
REM #################################

REM STEP 0 Windows command line stuff
:intro
	color 07
	echo.
	if %debug% == 1 echo [DEBUG MODE]
	echo ***********************************************************
	echo *
	echo * ----------------------------
	echo * FRAPS Encoding Batch MP4/HD
	echo * ----------------------------
	echo * 
	echo * Encode Fraps videos in the current directory (*.avi)
	echo * to mkv (MPEG4-AVC, FLAC)
	echo *
	echo * REQUIRES : FFmpeg, x264
	echo *
	echo ***********************************************************
	echo.
	goto :eof

:audio
	echo File processed : %1
	echo.
	echo.
	echo.
	echo ********************
	echo * Audio encoding...
	echo ********************		
	echo.
	echo ffmpeg -i %1 -vn -y "%~n1.flac"
		if %debug%==1 (
			REM Output encoding info to the CLI
			ffmpeg -i %1 -vn -y "%~n1.flac"
		)else (
			REM Output encoding info to a separate command window
			start /wait /min ffmpeg -i %1 -vn -y "%~n1.flac"
		)
		echo.
		if not %ERRORLEVEL% == 0 (
			echo ### Error in audio process ! ###			
			goto :error
		)
	if %debug% == 1 pause
	goto :eof

:video
	echo.
	echo.
	echo ********************
	echo * Video encoding...
	echo ********************
	echo.
	
	echo x264 --preset %3 --crf %2 --output "%~n1.264.mkv" %1
		if %debug% == 0 (
			REM Output encoding info to a separate window
			start /wait /min /%4 x264 --preset %3 --crf %2 --output "%~n1.264.mkv" %1
		)else (
			REM Output encoding info by x264 exe
			echo.
			x264 --preset %3 --crf %2 --output "%~n1.264.mkv" %1
		)		
		rem echo.
		
		if not %ERRORLEVEL% == 0 (
			echo ### Error in video process! ###			
			goto :error
		)
		
	if %debug% == 1 pause
	goto :eof
	
:muxing
	echo.
	echo.
	echo **************
	echo * Muxing...
	echo **************
	
	echo.
	echo ffmpeg -y -i "%~n1.264.mkv" -i "%~n1.flac" -vcodec copy -acodec copy "%~n1.mkv"
		if %debug% == 0 (
			REM Output encoding info to a separate window
			start /wait /min ffmpeg -y -i "%~n1.264.mkv" -i "%~n1.flac" -vcodec copy -acodec copy "%~n1.mkv"
		)else (
			REM Output encoding info by ffmpeg exe
			echo.
			ffmpeg -y -i "%~n1.264.mkv" -i "%~n1.flac" -vcodec copy -acodec copy "%~n1.mkv"
		)		
		echo.
		
		if not %ERRORLEVEL% == 0 (
			echo ### Error in muxing process! Check ffmpeg window to see what happened! ###			
			goto :error
		)
	
	rem add &(del %1) at the end of the next line if you want to del original FRAPS file too
	if %debug%==0 (del *.264.mkv) & (del *.flac) 
	goto :eof

:error
	rem Specifies color attribute of console output
	rem DOS text color light red
	color 0C
	echo *********************************
	echo * Erf! Something went wrong ... *
	echo *********************************
	pause
	rem uncoment next line if the scripts is ok for you.
	rem exit

:next
	rem Specifies color attribute of console output
	rem DOS text color light green
	color 0A
	echo.
	echo.
	echo *********************************
	echo * Successful video encoding !
	echo *********************************
	echo.
	if %debug% == 1 pause

	echo Processing next video in 5 seconds...
	ping localhost -n 6 > NUL
	cls
	goto :eof